<?php

namespace Admin\Controller;

/**
 * Description of GoodsController
 *
 * @author 阿明 symphp@foxmail.com
 */
class GoodsController extends \Think\Controller {

    //创建模型对象
    private $_model = null;

    protected function _initialize() {
        //构建数据
        $meta_titles = array(
            'index' => '商品管理',
            'add' => '商品添加',
            'edit' => '商品修改',
            'delete' => '商品删除',
        );
        $meta_title = $meta_titles[ACTION_NAME];
        //发送数据
        $this->assign('meta_title', $meta_title);
        //创建模型对象
        $this->_model = D('Goods');
    }

    /**
     * 创建一个公共函数用于传递数据
     */
    private function display_view() {
        //获取数据
        $data = D('GoodsCategory')->getList('id,name,parent_id');
        //将数据转换成json
        $jsondata = json_encode($data);
        //发送数据
        $this->assign('jsondata', $jsondata);
        //获取商品列表里的数据
        $brand = D('Brand')->getList('name,id');
        //发送到视图
        $this->assign('brand', $brand);
        //获取供货商发送到视图
        $Supplier = D('Supplier')->getLast('name,id');
        //发送到视图
        $this->assign('Supplier', $Supplier);
    }

    /**
     * 显示首页
     */
    public function index() {
        //准备数据
        $keyword = I('get.keyword');
        $category = I('get.cat_id');
        $brand = I('get.brand_id');
        $supplier = I('get.supplier_id');
        $goods_status = I('get.goods_status');
        $is_on_sale = I('get.is_on_sale');
        $cond = array();
        if($keyword){
            $cond['name'] = array('like','%'.$keyword.'%');
        }
        if($category){
            $cond['goods_category_id'] = $category;
        }
        if($brand){
            $cond['brand_id'] = $brand;
        }
        if($supplier){
            $cond['supplier_id'] = $supplier;
        }
        if($goods_status){
            $cond[] = 'goods_status&'.$goods_status;
        }
        if(strlen($is_on_sale)){
            $cond['is_on_sale'] = $is_on_sale;
        }
        
        
        $rows = $this->_model->getPageResult($cond);
        //获取对应表的数据
        $getTableInfo = $this->_model->getTableInfo();
        //获取商品状态数据
        $goods_status = $this->_model->goods_status;
        $is_on_sales = $this->_model->is_on_sales;
        //发送到视图
        $this->assign($getTableInfo);
        $this->assign($rows);
        //商品的状态
        $this->assign('goods_status',$goods_status);
        $this->assign('is_on_sales', $is_on_sales);
        $this->display();
    }

    /**
     * 显示添加页面
     */
    public function add() {
        if (IS_POST) {
            if ($this->_model->create() === FALSE) {
                $this->error(get_error($this->_model->getError()));
            };
            if($this->_model->goodsAdd()){
                $this->success('添加成功',U('index'));
            }else{
                 $this->error('添加失败',U('index'));
            };
        } else {
            //调用商品分类控制器
            $this->display_view();
            $this->display();
        }
    }

    /**
     * 商品编辑
     */
    public function edit($id) {
        if (IS_POST) {
            //1.收集数据
            $data = $this->_model->create();
            if($data === false){
                $this->error(get_error($this->_model->getError()));
            } else {
                if($this->_model->updateGoods()===false){
                    $this->error(get_error($this->_model->getError()));
                }else{
                     $this->success('修改成功',U('index'));
                }
            }
        }else{
            //通过id获取goods内容
            $row = $this->_model->getGoodsInfo($id);
            if($row===false){
                $this->error('商品不存在');
            } 
            //发送数据
            $this->assign('row',$row);
            //调用商品分类控制器
            $this->display_view();
            $this->display('add');
        }
    }
        
        /**
         * 删除商品
         * @param type $id
         */
        public function delete($id){
            //获取id
            $id = I('get.id');
            if($this->_model->getDelete($id)===false){
                $this->error(get_error($this->_model->getError()));
            }else{
                $this->success('删除成功');
            }
        }
        
    }




